********************************************************************
****************** THE ILLUSION OF STABLE FERTILITY PREFERENCES ****
********************************************************************

clear all

global folder `c(pwd)'
global data "$folder\data"
global output "$folder\output_appendix"
global logfiles "$folder\logfiles"
global fulldata "$data\ReplicationData_IofSFP_PS.dta"

* 1st time: ssc install labutil
set more off
 
******************************************************
******************* SETTINGS *************************
******************************************************
log using "$logfiles\logfile_appendix", replace 



********************************************************************
****************** TABLE A1 ****************************************
********************************************************************

use "$fulldata", clear
keep if women_KLPS1to3==1 // women in analysis sample (KLPS-1, 2, & 3)
keep if intchi_KLPS1>=0 & intchi_KLPS1<=8 & intchi_KLPS2<=8 & intchi_KLPS2>=0 & intchi_KLPS3>=0 & intchi_KLPS3<=8

gen weight_rounded=round(weight_adj_KLPS3)
expand weight_rounded

tab D_pref_KLPS3_1 intchi_KLPS2

gen pref_histories_KLPS1to3 = .
* DESIRED CHILDREN KLPS-1: 0
replace pref_histories_KLPS1to3 = 0 if intchi_KLPS1==0 & intchi_KLPS3==2 

* DESIRED CHILDREN KLPS-1: 1
replace pref_histories_KLPS1to3 = 1 if intchi_KLPS1==1 & intchi_KLPS3==2 

* DESIRED CHILDREN KLPS-1: 2
replace pref_histories_KLPS1to3 = 2 if intchi_KLPS1==2 & intchi_KLPS3==0
replace pref_histories_KLPS1to3 = 3 if intchi_KLPS1==2 & intchi_KLPS3==1
replace pref_histories_KLPS1to3 = 4 if intchi_KLPS1==2 & intchi_KLPS3==2
replace pref_histories_KLPS1to3 = 5 if intchi_KLPS1==2 & intchi_KLPS3==3
replace pref_histories_KLPS1to3 = 6 if intchi_KLPS1==2 & intchi_KLPS3==4
replace pref_histories_KLPS1to3 = 7 if intchi_KLPS1==2 & intchi_KLPS3==5

* DESIRED CHILDREN KLPS-1: 3
replace pref_histories_KLPS1to3 = 8 if intchi_KLPS1==3 & intchi_KLPS3==0
replace pref_histories_KLPS1to3 = 9 if intchi_KLPS1==3 & intchi_KLPS3==1
replace pref_histories_KLPS1to3 = 10 if intchi_KLPS1==3 & intchi_KLPS3==2
replace pref_histories_KLPS1to3 = 11 if intchi_KLPS1==3 & intchi_KLPS3==3
replace pref_histories_KLPS1to3 = 12 if intchi_KLPS1==3 & intchi_KLPS3==4
replace pref_histories_KLPS1to3 = 13 if intchi_KLPS1==3 & intchi_KLPS3==5
replace pref_histories_KLPS1to3 = 14 if intchi_KLPS1==3 & intchi_KLPS3==6

* DESIRED CHILDREN KLPS-1: 4
replace pref_histories_KLPS1to3 = 15 if intchi_KLPS1==4 & intchi_KLPS3==0
replace pref_histories_KLPS1to3 = 16 if intchi_KLPS1==4 & intchi_KLPS3==2
replace pref_histories_KLPS1to3 = 17 if intchi_KLPS1==4 & intchi_KLPS3==3
replace pref_histories_KLPS1to3 = 18 if intchi_KLPS1==4 & intchi_KLPS3==4
replace pref_histories_KLPS1to3 = 19 if intchi_KLPS1==4 & intchi_KLPS3==5
replace pref_histories_KLPS1to3 = 20 if intchi_KLPS1==4 & intchi_KLPS3==6
replace pref_histories_KLPS1to3 = 21 if intchi_KLPS1==4 & intchi_KLPS3==8

* DESIRED CHILDREN KLPS-1: 5
replace pref_histories_KLPS1to3 = 22 if intchi_KLPS1==5 & intchi_KLPS3==0
replace pref_histories_KLPS1to3 = 23 if intchi_KLPS1==5 & intchi_KLPS3==1
replace pref_histories_KLPS1to3 = 24 if intchi_KLPS1==5 & intchi_KLPS3==2
replace pref_histories_KLPS1to3 = 25 if intchi_KLPS1==5 & intchi_KLPS3==3
replace pref_histories_KLPS1to3 = 26 if intchi_KLPS1==5 & intchi_KLPS3==4
replace pref_histories_KLPS1to3 = 27 if intchi_KLPS1==5 & intchi_KLPS3==5
replace pref_histories_KLPS1to3 = 28 if intchi_KLPS1==5 & intchi_KLPS3==6
replace pref_histories_KLPS1to3 = 29 if intchi_KLPS1==5 & intchi_KLPS3==8

* DESIRED CHILDREN KLPS-1: 6
replace pref_histories_KLPS1to3 = 30 if intchi_KLPS1==6 & intchi_KLPS3==2
replace pref_histories_KLPS1to3 = 31 if intchi_KLPS1==6 & intchi_KLPS3==3
replace pref_histories_KLPS1to3 = 32 if intchi_KLPS1==6 & intchi_KLPS3==4
replace pref_histories_KLPS1to3 = 33 if intchi_KLPS1==6 & intchi_KLPS3==5
replace pref_histories_KLPS1to3 = 34 if intchi_KLPS1==6 & intchi_KLPS3==6

* DESIRED CHILDREN KLPS-1: 7
replace pref_histories_KLPS1to3 = 35 if intchi_KLPS1==7 & intchi_KLPS3==5

* DESIRED CHILDREN KLPS-1: 8
replace pref_histories_KLPS1to3 = 36 if intchi_KLPS1==8 & intchi_KLPS3==5

label define histories 0 "0 to 2" ///
1 "1 to 2" ///
2 "2 to 0" 3 "2 to 1" 4 "2 to 2" 5 "2 to 3" 6 "2 to 4" 7 "2 to 5" ///
8 "3 to 0" 9 "3 to 1"  10 "3 to 2"  11 "3 to 3"  12 "3 to 4"  13 "3 to 5"  14 "3 to 6" ///
15 "4 to 0"  16 "4 to 2"  17 "4 to 3"  18 "4 to 4"  19 "4 to 5"  20 "4 to 6"  21 "4 to 8"  ///
22 "5 to 0"  23 "5 to 1" 24 "5 to 2" 25 "5 to 3"  26 "5 to 4"  27 "5 to 5"  28 "5 to 6"  29 "5 to 8" ///
30 "6 to 2" 31 "6 to 3" 32 "6 to 4"  33 "6 to 5"  34 "6 to 6" ///
35 "7 to 5" ///
36 "8 to 5" 

label values pref_histories_KLPS1to3 histories 

tab pref_histories_KLPS1to3 intchi_KLPS2, cell

gen type_profiles = .
forvalues i=1(1)8 {
	replace type_profiles = 0 if intchi_KLPS1==`i' & intchi_KLPS2==`i' & intchi_KLPS3==`i'
	replace type_profiles = 1 if intchi_KLPS1==`i' & intchi_KLPS3==`i' & intchi_KLPS2!=`i'
}

replace type_profiles = 2 if intchi_KLPS3<intchi_KLPS1 & intchi_KLPS2>=intchi_KLPS3 & intchi_KLPS2<=intchi_KLPS1
replace type_profiles = 3 if intchi_KLPS3>intchi_KLPS1 & intchi_KLPS2<=intchi_KLPS3 & intchi_KLPS2>=intchi_KLPS1
replace type_profiles = 4 if type_profiles == .

label define profiletypes 0 "Stable" 1 "Vacillating" 2 "Decreasing" 3 "Increasing" 4 "Other Type"
label values type_profiles profiletypes

tab type_profiles


************************************
******** TABLE A.2. ****************
************************************

use "$fulldata", clear 
keep if women_KLPS1to3==1 // women in analysis sample (KLPS-1, 2, & 3)
gen weight_rounded=round(weight_adj_KLPS1F)
expand weight_rounded

**** 2 YEARS
tab k1f_s5_22expectababy2yrs // EXPECTATIONS
tab unknown_total_kids_2yrs_KLPS1 if k1f_s5_22expectababy2yrs==2 // CHILDREN WITHIN 2 YEARS CONDITIONAL ON EXPECTATIONS NOT TO HAVE A CHILD
tab unknown_total_kids_2yrs_KLPS1 if k1f_s5_22expectababy2yrs==1 // CHILDREN WITHIN 2 YEARS CONDITIONAL ON EXPECTATIONS TO HAVE A CHILD

**** 5 YEARS 
tab child_expect_5yrs // EXPECTATIONS
tab unknown_kids_5yrs_bothKLPS23 if child_expect_5yrs==2 // CHILDREN WITHIN 5 YEARS CONDITIONAL ON EXPECTATIONS NOT TO HAVE A CHILD
tab unknown_kids_5yrs_bothKLPS23 if child_expect_5yrs==1 // CHILDREN WITHIN 5 YEARS CONDITIONAL ON EXPECTATIONS TO HAVE A CHILD


************* INCONSISTENCY RATIO *****************
tab inconsistencies_2yrs
tab inconsistencies_5yrs



*********************************************************************
****************** FIGURE A1 ****************************************
*********************************************************************

**** BY MARRIAGE STATUS
graph set window fontface "Times New Roman"

***** MARRIED (RD 2)
use "$fulldata", clear
keep if in_KLPS23==1 // extended sample (in KLPS-2 & 3)
keep if female==1 & married_KLPS2==1
tab D_pref_KLPS3_2
foreach var in D_pref_KLPS3_2 {
keep if `var'>=-4 & `var'<=4
}
sum poschange_K2K3 [aw=weight_adj_KLPS3]
local N_married_1 = `r(N)'
local p_married_1 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3]
local mean_married_1 = `r(mean)'
local sd_married_1 = `r(sd)'

gen weight_rounded=round(weight_adj_KLPS3)
expand weight_rounded
sum D_pref_KLPS3_2
#delimit;	
histogram D_pref_KLPS3_2, discrete width(1)
	xlabel(-4(2)4, valuelabels) xtitle("Change in desires")
	ylabel (0(.1).5, format(%03.1f)) title("Married (Rd 2) (N=`N_married_1')", color(black)) 
	name(D_pref_KLPS3_2_married_w, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');
#delimit cr


***** NEWLY MARRIED
use "$fulldata", clear
keep if in_KLPS23==1 // extended sample (in KLPS-2 & 3)
keep if female==1 & married_KLPS2==0 & married_KLPS3==1
tab D_pref_KLPS3_2
foreach var in D_pref_KLPS3_2 {
keep if `var'>=-4 & `var'<=4
}
sum poschange_K2K3 [aw=weight_adj_KLPS3]
local N_married_2 = `r(N)'
local p_married_2 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3]
local mean_married_2 = `r(mean)'
local sd_married_2 = `r(sd)'

gen weight_rounded=round(weight_adj_KLPS3)
expand weight_rounded
sum D_pref_KLPS3_2
#delimit;	
histogram D_pref_KLPS3_2, discrete width(1)
	xlabel (-4(2)4, valuelabels) xtitle("Change in desires")
	ylabel (0(.1).5, format(%03.1f)) title("Newly Married (N=`N_married_2')", color(black)) 
	name(D_pref_KLPS3_2_newlywed_w, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');
#delimit cr


***** UNMARRIED (RD 3)
use "$fulldata", clear
keep if in_KLPS23==1 // extended sample (in KLPS-2 & 3)
keep if female==1 & married_KLPS2==0 & married_KLPS3==0
tab D_pref_KLPS3_2
foreach var in D_pref_KLPS3_2 {
keep if `var'>=-4 & `var'<=4
}
sum poschange_K2K3 [aw=weight_adj_KLPS3]
local N_married_3 = `r(N)' 
local p_married_3 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3]
local mean_married_3 = `r(mean)'
local sd_married_3 = `r(sd)'

gen weight_rounded=round(weight_adj_KLPS3)
expand weight_rounded
sum D_pref_KLPS3_2
#delimit;	
histogram D_pref_KLPS3_2, discrete width(1)
	xlabel (-4(2)4, valuelabels) xtitle("Change in desires")
	ylabel (0(.1).5, format(%03.1f)) title("Unmarried (Rd 3) (N=`N_married_3')", color(black)) 
	name(D_pref_KLPS3_2_solo_w, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');
#delimit cr


****** BREAK DOWN BY WHETHER PARENT BEFORE OR NOT ***
***** MOTHER (RD 2)
use "$fulldata", clear
keep if in_KLPS23==1 // extended sample (in KLPS-2 & 3)
keep if female==1 & ind_parent_KLPS2==1
tab D_pref_KLPS3_2
foreach var in D_pref_KLPS3_2 {
keep if `var'>=-4 & `var'<=4
}
sum poschange_K2K3 [aw=weight_adj_KLPS3]
local N_parent_1 = `r(N)'
local p_parent_1 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3]
local mean_parent_1 = `r(mean)'
local sd_parent_1 = `r(sd)'

gen weight_rounded=round(weight_adj_KLPS3)
expand weight_rounded
sum D_pref_KLPS3_2
#delimit;	
histogram D_pref_KLPS3_2, discrete width(1)
	xlabel (-4(2)4, valuelabels) xtitle("Change in desires")
	ylabel (0(.1).5, format(%03.1f)) title("Mother (Rd 2) (N=`N_parent_1')", color(black)) 
	name(D_pref_KLPS3_2_parent_w, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');
#delimit cr

***** NEW MOTHER 
use "$fulldata", clear
keep if in_KLPS23==1 // extended sample (in KLPS-2 & 3)
keep if female==1 & ind_parent_K2K3==1 
tab D_pref_KLPS3_2
foreach var in D_pref_KLPS3_2 {
keep if `var'>=-4 & `var'<=4
}
sum poschange_K2K3 [aw=weight_adj_KLPS3]
local N_parent_2 = `r(N)'
local p_parent_2 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3]
local mean_parent_2 = `r(mean)'
local sd_parent_2 = `r(sd)'


gen weight_rounded=round(weight_adj_KLPS3)
expand weight_rounded
sum D_pref_KLPS3_2
#delimit;	
histogram D_pref_KLPS3_2, discrete width(1)
	xlabel (-4(2)4, valuelabels) xtitle("Change in desires")
	ylabel (0(.1).5, format(%03.1f)) title("New Parent (N=`N_parent_2')", color(black)) 
	name(D_pref_KLPS3_2_newlymom_w, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');
#delimit cr


**** CHILDLESS 
use "$fulldata", clear
keep if in_KLPS23==1 // extended sample (in KLPS-2 & 3)
keep if female==1 & ind_parent_KLPS2==0 & ind_parent_KLPS3==0
tab D_pref_KLPS3_2
foreach var in D_pref_KLPS3_2 {
keep if `var'>=-4 & `var'<=4
}
sum poschange_K2K3 [aw=weight_adj_KLPS3]
local N_parent_3 = `r(N)'
local p_parent_3 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3]
local mean_parent_3 = `r(mean)'
local sd_parent_3 = `r(sd)'

gen weight_rounded=round(weight_adj_KLPS3)
expand weight_rounded
sum D_pref_KLPS3_2
#delimit;	
histogram D_pref_KLPS3_2, discrete width(1)
	xlabel (-4(2)4, valuelabels) xtitle("Change in desires")
	ylabel (0(.1).5, format(%03.1f)) title("Childless (N=`N_parent_3')", color(black))
	name(D_pref_KLPS3_2_nomom_w, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');

#delimit cr

*** OVERALL GRAPHS 
* ALL COMBINED
graph combine D_pref_KLPS3_2_married_w D_pref_KLPS3_2_newlywed_w D_pref_KLPS3_2_solo_w, ///
rows(1) ycommon xcommon graphregion(color(white)) name(women_marriage, replace)
graph export "$output\FigureA1a.pdf", as(pdf) replace
graph combine D_pref_KLPS3_2_parent_w D_pref_KLPS3_2_newlymom_w D_pref_KLPS3_2_nomom_w, ///
rows(1) ycommon xcommon graphregion(color(white)) name(women_parent, replace)
graph export "$output\FigureA1b.pdf", as(pdf) replace

* SHARES AND TESTS:
* MARRIAGE
display "test=`N_married_1'"
display `N_married_2'
display `p_married_1'
display `p_married_2'

foreach word in married parent {
display "WOMEN by `word': 1) long vs. recent, 2) recent vs. not yet, 3) long vs. not yet"

display "1) `word': long vs. recent"
prtesti `N_`word'_1' `p_`word'_1' `N_`word'_2' `p_`word'_2'
ttesti `N_`word'_1' `mean_`word'_1' `sd_`word'_1' `N_`word'_2' `mean_`word'_2' `sd_`word'_2'
display "2) `word': recent vs. not yet"
prtesti `N_`word'_2' `p_`word'_2' `N_`word'_3' `p_`word'_3'
ttesti `N_`word'_2' `mean_`word'_2' `sd_`word'_2' `N_`word'_3' `mean_`word'_3' `sd_`word'_3'
display "3) `word': long vs. not yet"
prtesti `N_`word'_1' `p_`word'_1' `N_`word'_3' `p_`word'_3'
ttesti `N_`word'_1' `mean_`word'_1' `sd_`word'_1' `N_`word'_3' `mean_`word'_3' `sd_`word'_3'
}


********************************************************************
****************** FIGURE A2 ***************************************
********************************************************************

** DESIRED CHILDREN
use "$fulldata", clear
rename weight_adj_KLPS1F weight_adj_KLPS1
forvalues i=1(1)3{
preserve
keep if women_KLPS1to3==1 // women in analysis sample (KLPS-1, 2, & 3)
gen weight_rounded=round(weight_adj_KLPS`i')
expand weight_rounded
sum intchi_KLPS`i'
local avg1 = round(`r(mean)', .01)
histogram intchi_KLPS`i', discrete width(1) ///
	xtitle("Number of children desired", size(medsmall)) ///
	ytitle("Density", size(medsmall)) ///
	ylabel(0(0.1)0.4, format(%03.1f)) ///
	title("Round `i'", color(black)) name(intchi_KLPS`i', replace) graphregion(color(white)) fcolor(dknavy) lcolor(white) xline(`r(mean)') ///
	text(0.35 7.5 "Mean: `avg1'", color(red)) 
restore
}
graph combine intchi_KLPS1 intchi_KLPS2 intchi_KLPS3, rows(1) ycommon ///
xcommon title("Number of children desired", color(black)) graphregion(color(white))
graph export "$output\FigureA2a.pdf", as(pdf) replace

** LIVING CHILDREN
use "$fulldata", clear
rename weight_adj_KLPS1F weight_adj_KLPS1
forvalues i=1(1)3{
preserve
keep if women_KLPS1to3==1 // women in analysis sample (KLPS-1, 2, & 3)
gen weight_rounded=round(weight_adj_KLPS`i')
expand weight_rounded
sum living_kids_KLPS`i'
local avg1 = round(`r(mean)', .01)
histogram living_kids_KLPS`i', discrete width(1) ///
	xlabel(0(1)6) ///
	ylabel(0(0.1)0.5, format(%03.1f)) ///
	xtitle("Number of living children") ///
	title("Round `i'", color(black)) name(living_KLPS`i', replace) graphregion(color(white)) fcolor(dknavy) lcolor(white) xline(`r(mean)') ///
	text(0.45 5 "Mean: `avg1'", color(red))
restore
}
graph combine living_KLPS1 living_KLPS2 living_KLPS3, rows(1) ycommon xcommon ///
title("Number of Children Alive", color(black)) graphregion(color(white))
graph export "$output\FigureA2b.pdf", as(pdf) replace


*********************************************************************
****************** FIGURE A3 ****************************************
*********************************************************************

use "$fulldata", clear

* summary statistics by group (extended sample)
eststo clear
bys age_KLPS2: eststo: quietly estpost summarize age_KLPS2 intchi_KLPS2 living_kids_KLPS2 [aw=weight_adj_KLPS2] if female==0 & age_KLPS2>=18 & age_KLPS2<=26 & intchi_KLPS2<=10 & living_kids_KLPS2<=10
bys age_KLPS2: eststo: quietly estpost summarize age_KLPS2 intchi_KLPS2 living_kids_KLPS2 [aw=weight_adj_KLPS2] if female==1 & age_KLPS2>=18 & age_KLPS2<=26 & intchi_KLPS2<=10 & living_kids_KLPS2<=10
bys age_KLPS3: eststo: quietly estpost summarize age_KLPS3 intchi_KLPS3 living_kids_KLPS3 [aw=weight_adj_KLPS3] if female==0 & age_KLPS3>=22 & age_KLPS3<=30 & intchi_KLPS3<=10 & living_kids_KLPS3<=10
bys age_KLPS3: eststo: quietly estpost summarize age_KLPS3 intchi_KLPS3 living_kids_KLPS3 [aw=weight_adj_KLPS3] if female==1 & age_KLPS3>=22 & age_KLPS3<=30 & intchi_KLPS3<=10 & living_kids_KLPS3<=10
esttab using "$output/FigureA3.csv", replace plain cells("mean") label nodepvar   


*********************************************************************
****************** FIGURE A4 ****************************************
*********************************************************************

use "$fulldata", clear
graph set window fontface "Times New Roman"
keep if in_KLPS3==1 & female==1 // extended sample (women in KLPS-2 & 3)
* exclude all with a dead birth/ death of child
keep if childdeath_K2K3==0 & deadbirth_K2K3==0

sum poschange_K2K3 [aw=weight_adj_KLPS3] if living_kids_KLPS3==intchi_KLPS2-1 & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4
local N_1 = `r(N)'
local p_1 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3] if living_kids_KLPS3==intchi_KLPS2-1 & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4
local N_1b = `r(N)'
local p_1b = `r(mean)'
local sd_1b = `r(sd)'

sum poschange_K2K3 [aw=weight_adj_KLPS3] if alldesiredchildren==0 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4
local N_2 = `r(N)'
local p_2 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3] if alldesiredchildren==0 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4
local N_2b = `r(N)'
local p_2b = `r(mean)'
local sd_2b = `r(sd)'

sum poschange_K2K3 [aw=weight_adj_KLPS3] if alldesiredchildren==1 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4
local N_3 = `r(N)'
local p_3 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3] if alldesiredchildren==1 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4
local N_3b = `r(N)'
local p_3b = `r(mean)'
local sd_3b = `r(sd)'

sum poschange_K2K3 [aw=weight_adj_KLPS3] if alldesiredchildren==2 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4
local N_4 = `r(N)'
local p_4 = `r(mean)'
sum D_pref_KLPS3_2 [aw=weight_adj_KLPS3] if alldesiredchildren==2 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4
local N_4b = `r(N)'
local p_4b = `r(mean)'
local sd_4b = `r(sd)'

prtesti `N_1' `p_1' `N_2' `p_2'
prtesti `N_2' `p_2' `N_3' `p_3'
prtesti `N_3' `p_3' `N_4' `p_4'
prtesti `N_2' `p_2' `N_4' `p_4'
ttesti `N_1b' `p_1b' `sd_1b' `N_2b' `p_2b' `sd_2b'
ttesti `N_2b' `p_2b' `sd_2b' `N_3b' `p_3b' `sd_3b'
ttesti `N_3b' `p_3b' `sd_3b' `N_4b' `p_4b' `sd_4b'
ttesti `N_2b' `p_2b' `sd_2b' `N_4b' `p_4b' `sd_4b'

gen weight_rounded=round(weight_adj_KLPS3)
expand weight_rounded

sum D_pref_KLPS3_2 if living_kids_KLPS3==intchi_KLPS2-1 & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4
#delimit;
histogram D_pref_KLPS3_2 if living_kids_KLPS3==intchi_KLPS2-1 & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4, discrete width(1)
	xlabel (-4(2)4, valuelabels)
	xtitle("Change in desires")
	ylabel (0(.1).6, format(%03.1f))
	title("One child away" "(N=`N_1')", color(black)) name(onechildaway, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');

sum D_pref_KLPS3_2 if alldesiredchildren==0 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4;
#delimit;	
histogram D_pref_KLPS3_2 if alldesiredchildren==0 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4, discrete width(1)
	xlabel (-4(2)4, valuelabels)
	xtitle("Change in desires")
	ylabel (0(.1).6, format(%03.1f))
	title("Mixed gender" "(N=`N_2')", color(black)) name(mixedgender, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');

sum D_pref_KLPS3_2 if alldesiredchildren==1 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4;
histogram D_pref_KLPS3_2 if alldesiredchildren==1 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4, discrete width(1)
	xlabel (-4(2)4, valuelabels)
	xtitle("Change in desires")
	ylabel (0(.1).6, format(%03.1f))
	title("Sons only" "(N=`N_3')", color(black)) name(onlyboys, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');

sum D_pref_KLPS3_2 if alldesiredchildren==2 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4;
histogram D_pref_KLPS3_2 if alldesiredchildren==2 & (living_kids_KLPS3==intchi_KLPS2) & D_pref_KLPS3_2<=4 & D_pref_KLPS3_2>=-4, discrete width(1)
	xlabel (-4(2)4, valuelabels)
	xtitle("Change in desires")
	ylabel (0(.1).6, format(%03.1f))
	title("Daughters only" "(N=`N_4')", color(black)) name(onlygirls, replace) fcolor(dknavy) lcolor(white) graphregion(color(white)) xline(`r(mean)');

#delimit cr
graph combine onechildaway mixedgender onlyboys onlygirls, rows(1) ycommon xcommon ///
title("Round 2 to 3", color(black)) graphregion(color(white))
graph export "$output\FigureA4.pdf", as(pdf) replace


*********************************************************************
****************** FIGURE A5 ****************************************
*********************************************************************

use "$fulldata", clear
keep if women_KLPS12==1 // analysis sample (in KLPS-1 & 2)

keep if excessfertility_R2K2<=6 & excessfertility_R2K2>=-6
keep if excessfertility_K1K2<=6 & excessfertility_K1K2>=-6

gen weight_rounded=round(weight_adj_KLPS2)
expand weight_rounded

tab excessfertility_K1K2 // EXCESS FERTILITY AS JUDGED AGAINST TRUE PAST DESIRES
tab excessfertility_R2K2 // EXCESS FERTILITY AS JUDGED AGAINST RECALLED PAST DESIRES
tab excessfertility_K2K2 // EXCESS FERTILITY AS JUDGED AGAINST CURRENT PAST DESIRES

twoway (histogram excessfertility_R2K2, discrete width(1) ///
	xtitle("") xlabel(-6(2)6) fcolor(dknavy) lcolor(white) graphregion(color(white))) ///
(histogram excessfertility_K1K2, discrete width(1) ///
	xtitle("") xlabel(-6(2)6) ///
	name(excess_R2K2, replace) fcolor(none) lcolor(red) graphregion(color(white))), ///
	legend(order(1 "Children (Round 2) - Recalled Desires (Round 2)" 2 "Children (Round 2) - Desires (Round 1)") rows(2)) ///
title("Actual and Recalled Excess Fertility", color(black)) 
graph export "$output\FigureA5.pdf", as(pdf) replace


*********************************************************************
****************** FIGURE A6 ****************************************
*********************************************************************
graph set window fontface "Times New Roman"

* RECALL DIRECTION
local i=0
graph drop _all
foreach var in married_KLPS1 ind_parent_KLPS1 {
use "$fulldata", clear
keep if women_KLPS12==1 // analysis sample (in KLPS-1 & 2)
keep if D_pref_KLPS2_1<=10 & D_pref_KLPS2_1>=-10
forvalues x=0/1 {
use "$fulldata", clear
keep if women_KLPS12==1 // analysis sample (in KLPS-1 & 2)
keep if D_pref_KLPS2_1<=10 & D_pref_KLPS2_1>=-10
forvalues z=0(1)2 {
preserve
keep if `var'==`x'
sum recalldirection [aw=weight_adj_KLPS2] if type_change==`z'
gen mean_1 = `r(mean)'
gen lb_1 = `r(mean)'-1.96*((`r(mean)'*(1-`r(mean)')/`r(N)')^0.5)
gen ub_1 = `r(mean)'+1.96*((`r(mean)'*(1-`r(mean)')/`r(N)')^0.5)
replace lb_1=0 if lb_1<0
replace ub_1=1 if ub_1>1
local N_`var'_type`z'_`x' = `r(N)'
local p_`var'_type`z'_`x' = `r(mean)'
keep if type_change==`z'
collapse (mean) mean_1 lb_1 ub_1, by(type_change)
tempfile file_`var'_`z'_`x'
save "file_`var'_`z'_`x'", replace
restore 
}
display "Recall Direction for `var'==`x': STABLE VS. LOWERED, then STABLE VS. INCREASED"
prtesti `N_`var'_type0_`x'' `p_`var'_type0_`x'' `N_`var'_type1_`x'' `p_`var'_type1_`x''
prtesti `N_`var'_type0_`x'' `p_`var'_type0_`x'' `N_`var'_type2_`x'' `p_`var'_type2_`x''

prtesti `N_`var'_type1_`x'' `p_`var'_type1_`x'' `N_`var'_type2_`x'' `p_`var'_type2_`x''
local p_`var'_`x' : di %4.3f 2*(normprob(-abs(r(z))))

use file_`var'_0_`x', clear
append using file_`var'_1_`x'
append using file_`var'_2_`x'

if `i'==0 {
local status "Unmarried (Rd 1)"
}
else if `i'==1 {
local status "Married (Rd 1)"
}
else if `i'==2 { 
local status "Childless (Rd 1)" 
}
else {
local status "Mother (Rd 1)" 
}
#delimit;
twoway (bar mean_1 type_change, ylabel(0(0.2)1, format(%03.1f)) xlabel(0(1)2, valuelabels) 
fcolor(gs10) lcolor(gs0) lpattern(solid) barwidth(0.4))
(rcap lb_1 ub_1 type_change, lcolor(black))
(scatteri 0.55 1.1 0.55 1.9, c(l) msym(S) lcolor(red) mcolor(red)), 
text(0.65 1.5 "p-value=`p_`var'_`x''") 
xtitle("") title("`status'", color(black)) t1title("Recall direction")  ytitle("Share") legend(off) graphregion(color(white)) name(plot_`var'_`x');
#delimit cr
local i=`i'+1
display "`i'"
}
}

* RECALL 0 CHANGE
local i=0
foreach var in married_KLPS1 ind_parent_KLPS1 {
use "$fulldata", clear
keep if women_KLPS12==1 // analysis sample (in KLPS-1 & 2)
keep if D_pref_KLPS2_1<=10 & D_pref_KLPS2_1>=-10
forvalues x=0/1 {
use "$fulldata", clear
keep if women_KLPS12==1 // analysis sample (in KLPS-1 & 2)
keep if D_pref_KLPS2_1<=10 & D_pref_KLPS2_1>=-10
forvalues z=0(1)2 {
preserve
keep if `var'==`x'
sum recallzerochange [aw=weight_adj_KLPS2] if type_change==`z'
gen mean_1 = `r(mean)'
gen lb_1 = `r(mean)'-1.96*((`r(mean)'*(1-`r(mean)')/`r(N)')^0.5)
gen ub_1 = `r(mean)'+1.96*((`r(mean)'*(1-`r(mean)')/`r(N)')^0.5)
replace lb_1 = 0 if lb_1<0
replace ub_1 = 1 if ub_1>1
local N_`var'_type`z'_`x' = `r(N)'
local p_`var'_type`z'_`x' = `r(mean)'
keep if type_change==`z'
collapse (mean) mean_1 lb_1 ub_1, by(type_change)
tempfile file_`var'_`z'_`x'
save "file_`var'_`z'_`x'", replace
restore 
}
display "Recall 0 Change for `var'==`x': STABLE VS. LOWERED, then STABLE VS. INCREASED"
prtesti `N_`var'_type0_`x'' `p_`var'_type0_`x'' `N_`var'_type1_`x'' `p_`var'_type1_`x''
prtesti `N_`var'_type0_`x'' `p_`var'_type0_`x'' `N_`var'_type2_`x'' `p_`var'_type2_`x''

prtesti `N_`var'_type1_`x'' `p_`var'_type1_`x'' `N_`var'_type2_`x'' `p_`var'_type2_`x''
local p_`var'_`x' : di %4.3f 2*(normprob(-abs(r(z))))

use file_`var'_0_`x', clear
append using file_`var'_1_`x'
append using file_`var'_2_`x'

#delimit;
twoway (bar mean_1 type_change, ylabel(0(0.2)1, format(%03.1f)) xlabel(0(1)2, valuelabels) 
fcolor(gs10) lcolor(gs0) lpattern(solid) barwidth(0.4))
(rcap lb_1 ub_1 type_change, lcolor(black))
(scatteri 0.94 1.1 0.94 1.9, c(l) msym(S) lcolor(red) mcolor(red)), 
text(1 1.5 "p-value=`p_`var'_`x''") 
xlabel(0 "Stable" 1 "Lowered" 2 "Increased")
xtitle("") t1title("Recall stable desires") ytitle("Share") legend(off) graphregion(color(white)) name(plot1_`var'_`x');
#delimit cr
local i=`i'+1
display "`i'"
}
display "Recall 0 Change: Stable for `var'==0/1"
prtesti `N_`var'_type0_0' `p_`var'_type0_0' `N_`var'_type0_1' `p_`var'_type0_1'
}

graph combine plot_married_KLPS1_0 plot_married_KLPS1_1 plot1_married_KLPS1_0 plot1_married_KLPS1_1, rows(2) ycommon xcommon plotregion(fcolor(white)) graphregion(color(white))
graph export "$output\FigureA6a.pdf", as(pdf) replace

graph combine plot_ind_parent_KLPS1_0 plot_ind_parent_KLPS1_1 plot1_ind_parent_KLPS1_0 plot1_ind_parent_KLPS1_1, rows(2) ycommon xcommon plotregion(fcolor(white)) graphregion(color(white))
graph export "$output\FigureA6b.pdf", as(pdf) replace

*********************************************************************
****************** FIGURE A7 ****************************************
*********************************************************************

use "$fulldata", clear
graph set window fontface "Times New Roman"

keep if women_KLPS12==1 // analysis sample (in KLPS-1 & 2)
foreach var in recalldirection recallzerochange {
sum `var' if D_pref_KLPS2_1==-2
local N_0 = r(N)
sum `var' if D_pref_KLPS2_1==-1
local N_1 = r(N)
sum `var' if D_pref_KLPS2_1==0
local N_2 = r(N)
sum `var' if D_pref_KLPS2_1==1
local N_3 = r(N)
sum `var' if D_pref_KLPS2_1==2
local N_4 = r(N)
}
foreach var in recalldirection recallzerochange {
forvalues z=-2(1)2 {
preserve
local i=2+`z'
sum `var' [aw=weight_adj_KLPS2] if D_pref_KLPS2_1==`z' 
gen mean_1 = `r(mean)'
gen lb_1 = `r(mean)'-1.96*((`r(mean)'*(1-`r(mean)')/`r(N)')^0.5)
gen ub_1 = `r(mean)'+1.96*((`r(mean)'*(1-`r(mean)')/`r(N)')^0.5)
replace lb_1 = 0 if lb_1<0
replace ub_1 = 1 if ub_1>1

local N_`var'_type`i' = `r(N)'
local p_`var'_type`i' = `r(mean)'
keep if D_pref_KLPS2_1==`z'
collapse (mean) mean_1 lb_1 ub_1, by(D_pref_KLPS2_1)
tempfile `var'_magnitude_`i'
save "`var'_magnitude_`i'", replace
restore 
}
prtesti `N_`var'_type0' `p_`var'_type0' `N_`var'_type1' `p_`var'_type1' 
local p1_`var' : di %4.3f 2*(normprob(-abs(r(z))))
display `p1'
prtesti `N_`var'_type3' `p_`var'_type3' `N_`var'_type4' `p_`var'_type4' 
local p2_`var' : di %4.3f 2*(normprob(-abs(r(z))))
display `p2'
}
use recalldirection_magnitude_0, clear
forvalues t=1(1)4 {
append using recalldirection_magnitude_`t'
}
keep if D_pref_KLPS2_1<=2 & D_pref_KLPS2_1>=-2
#delimit;
twoway (bar mean_1 D_pref_KLPS2_1, ylabel(0(0.2)1, format(%03.1f)) xlabel(-2(1)2, valuelabels) 
fcolor(gs10) lcolor(gs0) lpattern(solid) barwidth(0.4))
(rcap lb_1 ub_1 D_pref_KLPS2_1, lcolor(black))
(scatteri 0.7 -1.9 0.7 -1.1, c(l) msym(S) lcolor(red) mcolor(red))
(scatteri 0.7 1.1 0.7 1.9, c(l) msym(S) lcolor(red) mcolor(red)),  
text(0.75 -1.5 "p-value=`p1_recalldirection'") text(0.75 1.5 "p-value=`p2_recalldirection'")
xtitle("Change in desires") title("Recall direction", color(black)) ytitle("Share") legend(off) graphregion(color(white)) name(plot3);
graph export "$output\FigureA7a.pdf", as(pdf) replace;
#delimit cr

use recallzerochange_magnitude_0, clear
forvalues t=1(1)4 {
append using recallzerochange_magnitude_`t'
}
keep if D_pref_KLPS2_1<=2 & D_pref_KLPS2_1>=-2

#delimit;
twoway (bar mean_1 D_pref_KLPS2_1, ylabel(0(0.2)1, format(%03.1f)) xlabel(-2(1)2)
fcolor(gs10) lcolor(gs0) lpattern(solid) barwidth(0.4))
(rcap lb_1 ub_1 D_pref_KLPS2_1, lcolor(black))
(scatteri 0.92 -1.9 0.92 -1.1, c(l) msym(S) lcolor(red) mcolor(red))
(scatteri 0.92 1.1 0.92 1.9, c(l) msym(S) lcolor(red) mcolor(red)), 
text(0.97 -1.5 "p-value=`p1_recallzerochange'") text(0.97 1.5 "p-value=`p2_recallzerochange'")
xtitle("Change in desires") title("Recall stable desires", color(black)) ytitle("Share") legend(off) graphregion(color(white)) name(plot4);
graph export "$output\FigureA7b.pdf", as(pdf) replace;
#delimit cr

prtesti `N_recalldirection_type0' `p_recalldirection_type0' `N_recalldirection_type4' `p_recalldirection_type4' 
prtesti `N_recalldirection_type1' `p_recalldirection_type1' `N_recalldirection_type3' `p_recalldirection_type3' 
prtesti `N_recallzerochange_type0' `p_recallzerochange_type0' `N_recallzerochange_type4' `p_recallzerochange_type4' 
prtesti `N_recallzerochange_type1' `p_recallzerochange_type1' `N_recallzerochange_type3' `p_recallzerochange_type3' 


log close 
